//
//  EditPhotoView.m
//  MrWrite
//
//  Created by lilg on 13-6-28.
//  Copyright (c) 2013年 Founder. All rights reserved.
//

#import "EditPhotoView.h"
#import "EditPhotoViewCell.h"

#define kBlueDotImageViewOffset 25.0f
#define kFilterCellHeight 72.0f
#define kBlueDotAnimationTime 0.2f
#define kFilterTableViewAnimationTime 0.2f
#define kGPUImageViewAnimationOffset 27.0f

@interface EditPhotoView ()<UITableViewDataSource, UITableViewDelegate,UIScrollViewDelegate>
@property (nonatomic, retain)UIButton         *backToCameraBtn;
@property (nonatomic, retain)UIButton         *savePhotoBtn;
@property (nonatomic, retain)UIImageView  *blueDotImageView;
@property (nonatomic, assign)CGPoint                   loactionPoint;

@end

@implementation EditPhotoView
@synthesize delegate;
@synthesize backToCameraBtn;
@synthesize savePhotoBtn;
@synthesize blueDotImageView;
@synthesize loactionPoint;

-(void)dealloc
{
  UIRelease(backToCameraBtn);
  UIRelease(blueDotImageView);
  UIRelease(savePhotoBtn);
  [super dealloc];
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
      [self initViews];
    }
    return self;
}

-(void)initViews
{
  self.backgroundColor = UIClearColor;
  backToCameraBtn = [[UIButton alloc]initWithFrame:CGRectMake(20, 0, 44, 44)];
  backToCameraBtn.backgroundColor = UIClearColor;
  [backToCameraBtn setTitle:@"返回" forState:UIControlStateNormal];
  [backToCameraBtn setTitleColor:UIBlackColor  forState:UIControlStateNormal];
  backToCameraBtn.tag = BackToCameraBtnTag;
  [backToCameraBtn addTarget:self action:@selector( EditViewButtonClick:) forControlEvents:UIControlEventTouchUpInside];
  [self addSubview:backToCameraBtn];
  
  savePhotoBtn = [[UIButton alloc]initWithFrame:CGRectMake(240, 0, 44, 44)];
  savePhotoBtn.backgroundColor = UIClearColor;
  [savePhotoBtn setTitle:@"使用" forState:UIControlStateNormal];
  [savePhotoBtn setTitleColor:UIBlackColor  forState:UIControlStateNormal];
  savePhotoBtn.tag = SharePhotoBtnTag;
  [savePhotoBtn addTarget:self action:@selector( EditViewButtonClick:) forControlEvents:UIControlEventTouchUpInside];
  [self addSubview:savePhotoBtn];
  
  UITableView *filtersTableView = [[UITableView alloc] initWithFrame:CGRectMake(124, -144, 72, 320) style:UITableViewStylePlain];
  filtersTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  filtersTableView.showsVerticalScrollIndicator = NO;
  filtersTableView.delegate = self;
  filtersTableView.dataSource = self;
  filtersTableView.transform	= CGAffineTransformMakeRotation(-M_PI/2);
  filtersTableView.backgroundView = nil;
  filtersTableView.backgroundColor = UIClearColor;
  
  blueDotImageView = [[UIImageView alloc]initWithFrame:CGRectMake(-3, kBlueDotImageViewOffset + 4, 21, 11)];
  blueDotImageView.image = [UIImage imageNamed:@"glCameraSelectedFilter"];
  blueDotImageView.transform = CGAffineTransformMakeRotation(-M_PI/2);
  [filtersTableView addSubview:blueDotImageView];
  
 UIImageView *cameraTrayImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 43, 320, 29)];
  cameraTrayImageView.image = [UIImage imageNamed:@"glCameraTray"];
  UIView  *filterTableViewContainerView = [[UIView alloc]initWithFrame:CGRectMake(0, 400, 320, 72)];
  [filterTableViewContainerView addSubview:cameraTrayImageView];
  [filterTableViewContainerView addSubview:filtersTableView];
  [self addSubview:filterTableViewContainerView];
  [cameraTrayImageView release];
  [filterTableViewContainerView release];
}

-(void)EditViewButtonClick:(id)sender
{
  [delegate EditPhotoViewDelegateButtonClickEvents:[sender tag]];
}

#pragma mark - Filters TableView Delegate & Datasource methods
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  return kFilterCellHeight;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  return 18;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  static NSString *filtersTableViewCellIdentifier = @"filtersTableViewCellIdentifier";
  EditPhotoViewCell *cell = [tableView dequeueReusableCellWithIdentifier: filtersTableViewCellIdentifier];
  if (cell == nil) {
    cell = [[[EditPhotoViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:filtersTableViewCellIdentifier]autorelease];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
  }
  NSString *imageName;
  switch ([indexPath row])
  {
    case 0: {
      imageName = @"DSFilterTileNormal";
      break;
    }
    case 1: {
      imageName = @"DSFilterTileAmaro";
      break;
    }
    case 2: {
      imageName = @"DSFilterTileRise";
      break;
    }
    case 3: {
      imageName = @"DSFilterTileHudson";
      break;
    }
    case 4: {
      imageName = @"DSFilterTileXpro2";
      break;
    }
    case 5: {
      imageName = @"DSFilterTileSierra";
      break;
    }
    case 6: {
      imageName = @"DSFilterTileLomoFi";
      break;
    }
    case 7: {
      imageName = @"DSFilterTileEarlybird";
      break;
    }
    case 8: {
      imageName = @"DSFilterTileSutro";
      break;
    }
    case 9: {
      imageName = @"DSFilterTileToaster";
      break;
    }
    case 10: {
      imageName = @"DSFilterTileBrannan";
      break;
    }
    case 11: {
      imageName = @"DSFilterTileInkwell";
      break;
    }
    case 12: {
      imageName = @"DSFilterTileWalden";
      break;
    }
    case 13: {
      imageName = @"DSFilterTileHefe";
      break;
    }
    case 14: {
      imageName = @"DSFilterTileValencia";
      break;
    }
    case 15: {
      imageName = @"DSFilterTileNashville";
      break;
    }
    case 16: {
      imageName = @"DSFilterTile1977";
      break;
    }
    case 17: {
      imageName = @"DSFilterTileLordKelvin";
      break;
    }
    default: {
      imageName = @"DSFilterTileNormal";
      break;
    }
  }
  [cell setFilterImageViewImage:[UIImage imageNamed:imageName]];
  return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  [delegate EditPhotoViewDelegateChangeFilterType:[indexPath row]];
  CGRect cellRect = [tableView rectForRowAtIndexPath:indexPath];
  CGRect tempRect = self.blueDotImageView.frame;
  tempRect.origin.y = cellRect.origin.y + kBlueDotImageViewOffset;
  
  [UIView animateWithDuration:kBlueDotAnimationTime animations:^()
  {
    self.blueDotImageView.frame = tempRect;
  }completion:^(BOOL finished)
  {
  }];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
